home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / archivers / xpk / xpk_source / xpkmaster / libinit.c < prev    next >
C/C++ Source or Header  |  1999-06-14  |  8KB  |  261 lines

  1. #ifndef XPKMASTER_LIBINIT_C
  2. #define XPKMASTER_LIBINIT_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        libinit.c
  7.     Main:        xpkmaster
  8.     Versionstring:    $VER: libinit.c 1.2 (09.05.1998)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    all the library initialization stuff
  12.  
  13.  1.0   21.02.98 : first version, based on libdata.a and work of Gunther Nikl
  14.  1.1   06.05.98 : added FreeMem for LibInit function
  15.  1.2   09.05.98 : added SysBase
  16. */
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/locale.h>
  20. #include <exec/resident.h>
  21. #include <exec/initializers.h>
  22. #include "xpkmaster.h"
  23. #include "texts.h"
  24. #include "version.h"
  25.  
  26. struct LibInitData {
  27.  UBYTE i_Type;     UBYTE o_Type;     UBYTE  d_Type;    UBYTE p_Type;
  28.  UBYTE i_Name;     UBYTE o_Name;     STRPTR d_Name;
  29.  UBYTE i_Flags;    UBYTE o_Flags;    UBYTE  d_Flags;    UBYTE p_Flags;
  30.  UBYTE i_Version;  UBYTE o_Version;  UWORD  d_Version;
  31.  UBYTE i_Revision; UBYTE o_Revision; UWORD  d_Revision;
  32.  UBYTE i_IdString; UBYTE o_IdString; STRPTR d_IdString;
  33.  ULONG endmark;
  34. };
  35.  
  36. /************************************************************************/
  37.  
  38. LONG ReturnError(void);
  39. static void CloseLibraries(void);
  40. static void LocaleStrings(STRPTR *, ULONG, ULONG);
  41.  
  42. ULONG LibReserved(void);
  43. ASM(BPTR) LibExpunge(REG(a6, struct Library *));
  44. ASM(BPTR) LibClose(REG(a6, struct Library *));
  45. ASM(struct Library *) LibOpen(REG(a6, struct Library *));
  46. ASM(struct Library *) LibInit(REG(a0, BPTR), REG(d0, struct Library *),
  47.     REG(a6, struct ExecBase *));
  48.  
  49. /************************************************************************/
  50.  
  51. struct DosLibrary *        DOSBase        = 0;
  52. struct IntuitionBase *        IntuitionBase    = 0;
  53. struct UtilityBase *        UtilityBase    = 0;
  54. struct Library *        GadToolsBase    = 0;
  55. struct LocaleBase *        LocaleBase    = 0;
  56. struct Library *        XpkBase        = 0;
  57. struct ExecBase *        SysBase        = 0;
  58. static struct Catalog *     Catalog        = 0;
  59. static BPTR            SegList        = 0;
  60.  
  61. /************************************************************************/
  62.  
  63. /* First executable routine of this library; must return an error
  64.    to the unsuspecting caller */
  65. LONG ReturnError(void)
  66. {
  67.   return -1;
  68. }
  69.  
  70. /************************************************************************/
  71.  
  72. /* The mandatory reserved library function */
  73. ULONG LibReserved(void)
  74. {
  75.   return 0;
  76. }
  77.  
  78. /* Close the library, as called by CloseLibrary() */
  79. ASM(BPTR) LibClose(REG(a6, struct Library * xpkLib))
  80. {
  81.   if(!(--xpkLib->lib_OpenCnt))
  82.   {
  83.     if(xpkLib->lib_Flags & LIBF_DELEXP)
  84.       return LibExpunge(xpkLib);
  85.   }
  86.   return 0;
  87. }
  88.  
  89. /* Open the library, as called via OpenLibrary() */
  90. ASM(struct Library *) LibOpen(REG(a6, struct Library * xpkLib))
  91. {
  92.   /* Prevent delayed expunge and increment opencnt */
  93.   xpkLib->lib_Flags &= ~LIBF_DELEXP;
  94.   xpkLib->lib_OpenCnt++;
  95.  
  96.   return xpkLib;
  97. }
  98.  
  99. /* Expunge the library, remove it from memory */
  100. ASM(BPTR) LibExpunge(REG(a6, struct Library * xpkLib))
  101. {
  102.   if(!xpkLib->lib_OpenCnt)
  103.   {
  104.     CloseLibraries();
  105.  
  106.     /* Remove the library from the public list */
  107.     Remove((struct Node *) xpkLib);
  108.  
  109.     /* Free the vector table and the library data */
  110.     FreeMem((STRPTR) xpkLib - xpkLib->lib_NegSize, xpkLib->lib_NegSize +
  111.     xpkLib->lib_PosSize);
  112.  
  113.     return SegList;
  114.   }
  115.   else
  116.     xpkLib->lib_Flags |= LIBF_DELEXP;
  117.  
  118.   /* Return the segment pointer, if any */
  119.   return 0;
  120. }
  121.  
  122. /* Closes all the libraries opened by LibrarySetup() */
  123. static void CloseLibraries(void)
  124. {
  125.   if(LocaleBase)
  126.   {
  127.     if(Catalog)
  128.       CloseCatalog(Catalog);
  129.     CloseLibrary((struct Library *) LocaleBase);
  130.   }
  131.   if(GadToolsBase)
  132.     CloseLibrary(GadToolsBase);
  133.   if(UtilityBase)
  134.     CloseLibrary((struct Library *) UtilityBase);
  135.   if(IntuitionBase)
  136.     CloseLibrary((struct Library *) IntuitionBase);
  137.   if(DOSBase)
  138.     CloseLibrary((struct Library *) DOSBase);
  139. }
  140.  
  141. /* Initialize library */
  142. ASM(struct Library *) LibInit(REG(a0, BPTR seglist),
  143. REG(d0, struct Library * xpkLib), REG(a6, struct ExecBase *sysbase))
  144. {
  145.   /* Remember the segment pointer */
  146.   SegList = seglist;
  147.  
  148.   /* get required data */
  149.   SysBase = sysbase;
  150.  
  151.   if((DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37)))
  152.   {
  153.     if((IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37)))
  154.     {
  155.       if((UtilityBase = (struct UtilityBase *) OpenLibrary("utility.library", 37)))
  156.       {
  157.         if((GadToolsBase = OpenLibrary("gadtools.library", 37)))
  158.         {
  159.           if((LocaleBase = (struct LocaleBase *) OpenLibrary("locale.library", 38)))
  160.           {
  161.             if((Catalog = OpenCatalog(0, "xpkmaster.catalog",
  162.             OC_Version, 2, TAG_DONE)))
  163.             {
  164.           LocaleStrings(strings, LOCALE_STRINGSTART, LOCALE_STRINGCNT);
  165.           LocaleStrings(XpkErrs, LOCALE_ERRSTRINGSTART, LOCALE_ERRSTRINGCNT);
  166.         }
  167.           }
  168.           return (XpkBase = xpkLib);
  169.         }
  170.       }
  171.     }
  172.     CloseLibraries();
  173.   }
  174.  
  175.   FreeMem((STRPTR) xpkLib - xpkLib->lib_NegSize, xpkLib->lib_NegSize +
  176.   xpkLib->lib_PosSize);
  177.  
  178.   return 0;
  179. }
  180.  
  181. static void LocaleStrings(STRPTR * string, ULONG start, ULONG count)
  182. {
  183.   while(count--)
  184.   {
  185.     *string = GetCatalogStr(Catalog, start++, *string);
  186.     ++string;
  187.   }
  188. }
  189.  
  190. /************************************************************************/
  191.  
  192. /* This is the table of functions that make up the library. The first
  193.    four are mandatory, everything following it are user callable
  194.    routines. The table is terminated by the value -1. */
  195.  
  196. static const APTR LibVectors[20] = {
  197.   LibOpen,
  198.   LibClose,
  199.   LibExpunge,
  200.   LibReserved,
  201.   LibReserved,
  202.   LIBXpkExamine,
  203.   LIBXpkPack,
  204.   LIBXpkUnpack,
  205.   LIBXpkOpen,
  206.   LIBXpkRead,
  207.   LIBXpkWrite,
  208.   LIBXpkSeek,
  209.   LIBXpkClose,
  210.   LIBXpkQuery,
  211.   LIBXpkAllocObject,
  212.   LIBXpkFreeObject,
  213.   LIBXpkPrintFault,
  214.   LIBXpkFault,
  215.   LIBXpkPassRequest,
  216.   (APTR)-1
  217. };
  218.  
  219. static const struct LibInitData LibInitData = {
  220.  0xA0, (UBYTE) OFFSET(Node,    ln_Type),      NT_LIBRARY,         0,
  221.  0x80, (UBYTE) OFFSET(Node,    ln_Name),      LIBNAME,
  222.  0xA0, (UBYTE) OFFSET(Library, lib_Flags),    LIBF_SUMUSED|LIBF_CHANGED, 0,
  223.  0x90, (UBYTE) OFFSET(Library, lib_Version),  VERSION,
  224.  0x90, (UBYTE) OFFSET(Library, lib_Revision), REVISION,
  225.  0x80, (UBYTE) OFFSET(Library, lib_IdString), IDSTRING,
  226.  0
  227. };
  228.  
  229. /* The following data structures and data are responsible for
  230.    setting up the Library base data structure and the library
  231.    function vector. */
  232.  
  233. static const ULONG LibInitTable[4] = {
  234.   (ULONG)sizeof(struct Library), /* Size of the base data structure */
  235.   (ULONG)LibVectors,             /* Points to the function vector */
  236.   (ULONG)&LibInitData,           /* Library base data structure setup table */
  237.   (ULONG)LibInit                 /* The address of the routine to do the setup */
  238. };
  239.  
  240. /************************************************************************/
  241.  
  242. /* The library loader looks for this marker in the memory
  243.    the library code and data will occupy. It is responsible
  244.    setting up the Library base data structure.
  245. */
  246.  
  247. static const struct Resident RomTag = {
  248.   RTC_MATCHWORD,                /* Marker value. */
  249.   (struct Resident *)&RomTag,   /* This points back to itself. */
  250.   (struct Resident *)&RomTag+1, /* This points behind this marker. */
  251.   RTF_AUTOINIT,                 /* The Library should be set up according to the given table. */
  252.   VERSION,                      /* The version of this Library. */
  253.   NT_LIBRARY,                   /* This defines this module as a Library. */
  254.   0,                            /* Initialization priority of this Library; unused. */
  255.   LIBNAME,                      /* Points to the name of the Library. */
  256.   IDSTRING,                     /* The identification string of this Library. */
  257.   (APTR)&LibInitTable           /* This table is for initializing the Library. */
  258. };
  259.  
  260. #endif /* XPKMASTER_LIBINIT_C */
  261.